import { post } from 'plugins/axios'
import { date, Dialog, QSpinnerGears } from 'quasar'

export function save ({state, commit}) {
  Dialog.create({
    title: '新增主机',
    message: '确定新增主机？',
    ok: '提交',
    cancel: '再想想'
  }).then(() => {
    state.loading.show({spinner: QSpinnerGears})
    state.fields.terminal_time = date.formatDate(state.fields.terminal_time, 'YYYY-MM-DD')
    post('/hosts/',
      state.fields
    ).then((response) => {
      commit('saveSuccess')
    }).catch((error) => {
      if (error.response.data) {
        commit('saveFail', error.response.data)
      }
    })
  }).catch(() => {
    console.log('stay')
  })
}
